package com.book.bookmanager.controller;


import com.book.bookmanager.pojo.Book;
import com.book.bookmanager.pojo.Person;
import com.book.bookmanager.pojo.User;
import com.book.bookmanager.service.personService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("person")
public class personController {
	@Autowired
	private personService ps;


	@GetMapping("queryByName")
	public String queryByName(Model model, HttpSession session) {

		/**
		 * 存入model
		 */
		String user1 = (String) session.getAttribute("user");
		System.out.println(user1);
		Person person = ps.queryByName(user1);
		System.out.println(person);
		model.addAttribute("person", person);
		return "showPerson";
	}

	@RequestMapping("remove")
	public String remove(String name) {
		try {
			ps.remove(name);
			return "redirect:/person/queryByName";
		} catch (Exception e) {
			return "error";
		}

	}

	@RequestMapping("toUpdate")
	public String toUpdate(HttpSession session, Model model) {
		String user1 = (String) session.getAttribute("user");
		Person person = ps.queryByName(user1);
		model.addAttribute("person", person);
		return "updatePerson";
	}

	@RequestMapping("changePerson")
	public String changePerson(Person person) {
		try {
			ps.changePerson(person);
			return "redirect:/person/queryByName";
		} catch (Exception e) {
			return "error";
		}
	}
}
